package lab3;

import java.util.Scanner;

public class Encryption {

	char[][] key;
	
	
	
	public Encryption() 
	{
		super();
		key=new char[5][5];
		enterKey();
	}

	private void enterKey()
	{
		Scanner sc = new Scanner(System.in);
		for (int i = 0; i<5; i++)
		{
			for (int j = 0; j<5; j++)
			{
				key[i][j]=sc.next().charAt(0);
			}
		}

	}

	private String encrypt (String word)
	{
		if (word.length()%2==1) word=word + "A";
		for (int i = 0; i<word.length(); i++)
		{
			if (word.charAt(i)>'Z'||word.charAt(i)<'A')
			{
				System.out.println("Not a valid word;");
				return "NVW";
			}
			if (word.charAt(i)=='Z')
			{
				if (i==0) word="Y" + word.substring(1, word.length());
				else if (i==word.length()-1) word=word.substring(0, word.length()-2) + "Y";
				else word=word.substring(0,i-1) + "Y" + word.substring(i-1,word.length()); 
			}
		}
		String encryptedWord = new String();
		for (int i = 0; i<word.length()/2; i++)
		{
			encryptedWord+=encryptCouple(word.substring(2*i, 2*i+2));
		}
		return encryptedWord;
	}
	
	private String decrypt (String word)
	{
		for (int i = 0; i<word.length(); i++)
		{
			if (word.charAt(i)>'Y'||word.charAt(i)<'A')
			{
				System.out.println("Not a valid word;");
				return "NVW";
			}
		}
		String decryptedWord = new String();
		for (int i = 0; i<word.length()/2; i++)
		{
			decryptedWord+=decryptCouple(word.substring(2*i, 2*i+2));
		}
		return decryptedWord;
	}
	
	private String decryptCouple (String letters)
	{
		int x0=0, x1=0, y0=0, y1=0;
		for (int i = 0; i<5; i++)
		{
			for (int j = 0; j<5; j++)
			{
				if (letters.charAt(0)==key[i][j])
				{
					x0=i;
					y0=j;
				}
				else if (letters.charAt(1)==key[i][j])
				{
					x1=i;
					y1=j;
				}
			}
		}
		if ((x0!=x1)&&(y0!=y1))
		{
			char letter0=key[x0][y1];
			char letter1=key[x1][y0];
			letters=""+letter0+letter1;
		}
		else if ((x0!=x1)&&(y0==y1))
		{
			x0--;
			x1--;
			if(x0==-1) x0=4;
			if(x1==-1) x1=4;
			char letter0=key[x0][y0];
			char letter1=key[x1][y1];
			letters=""+letter0+letter1;
		}
		else
		{
			y0++;
			y1++;
			if (y0==5) y0=0;
			if (y1==5) y1=0;
			char letter0=key[x0][y0];
			char letter1=key[x1][y1];
			letters=""+letter0+letter1;
		}
		return letters;
	}
	
	private String encryptCouple (String letters)
	{
		int x0=0, x1=0, y0=0, y1=0;
		for (int i = 0; i<5; i++)
		{
			for (int j = 0; j<5; j++)
			{
				if (letters.charAt(0)==key[i][j])
				{
					x0=i;
					y0=j;
				}
				else if (letters.charAt(1)==key[i][j])
				{
					x1=i;
					y1=j;
				}
			}
		}
		if ((x0!=x1)&&(y0!=y1))
		{
			char letter0=key[x0][y1];
			char letter1=key[x1][y0];
			letters=""+letter0+letter1;
		}
		else if ((x0!=x1)&&(y0==y1))
		{
			x0++;
			x1++;
			if(x1==5) x1=0;
			if(x0==5) x0=0;
			char letter0=key[x0][y0];
			char letter1=key[x1][y1];
			letters=""+letter0+letter1;
		}
		else
		{
			y0--;
			y1--;
			if (y0==-1) y0=4;
			if (y1==-1) y1=4;
			char letter0=key[x0][y0];
			char letter1=key[x1][y1];
			letters=""+letter0+letter1;
		}
		
		return letters;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Encryption enc = new Encryption();
		Scanner sc = new Scanner(System.in);
		String word = sc.nextLine();
		System.out.println("The input word is " + word);
		String encryptedWord= enc.encrypt(word);
		if (encryptedWord=="NVW") return;
		System.out.println("The encrypted word is " + encryptedWord);
		String decryptedWord=enc.decrypt(encryptedWord);
		System.out.println("The decrypted word us " + decryptedWord);
	}

}
